import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BullModule } from '@nestjs/bull';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NovelParseProcessor } from '../novel/processors/novel-parse.processor';
import { NovelParseTask } from '../novel/entities/novel-parse-task.entity';
import { Novel } from '../novel/entities/novel.entity';
import { NovelChapter } from '../novel/entities/novel-chapter.entity';
import { CategoryModule } from '../category/category.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([NovelParseTask, Novel, NovelChapter]),
    CategoryModule,
    BullModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        redis: {
          host: configService.get<string>('REDIS_HOST', '124.222.38.18'),
          port: configService.get<number>('REDIS_PORT', 6379),
          password: configService.get<string>('REDIS_PASSWORD', 'q13812833598'),
          db: configService.get<number>('REDIS_DB', 0),
        },
        defaultJobOptions: {
          removeOnComplete: 10, // 保留最近10个完成的任务
          removeOnFail: 50, // 保留最近50个失败的任务
          attempts: 3, // 默认重试次数
          backoff: {
            type: 'exponential',
            delay: 2000, // 初始延迟2秒
          },
        },
      }),
      inject: [ConfigService],
    }),
    BullModule.registerQueue({
      name: 'novel-parse', // 小说解析队列
    }),
  ],
  controllers: [],
  providers: [NovelParseProcessor],
  exports: [BullModule],
})
export class QueueModule {}
